#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <stdbool.h>  

// 定义结构体来存储收入和支出信息  
typedef struct {  
    char description[100];  
    double amount;  
    char type[10]; // "income" 或 "expense"  
} Transaction;  

// 定义结构体来存储预算信息  
typedef struct {  
    double budget;  
    double totalSpent;  
} Budget;  

// 定义数组来存储交易信息（为了简单起见，这里使用固定大小的数组）  
#define MAX_TRANSACTIONS 100  
Transaction transactions[MAX_TRANSACTIONS];  
int transactionCount = 0;  

// 创建预算实例  
Budget monthlyBudget = {0, 0};  

// 函数声明  
void recordIncome();  
void recordExpense();  
void displayMenu();  
void printAllTransactions();  
void searchTransaction();  
void clearInputBuffer();  
void setBudget();  
void displayRemainingBudget();  
void displayMonthlyStatistics();  

int main() {  
    int choice;  

    while (1) {  
        displayMenu();  
        printf("请输入你的选择: ");  
        scanf("%d", &choice);  
        clearInputBuffer(); // 清除输入缓冲区中的可能残留字符  

        switch (choice) {  
            case 1:  
                recordIncome();  
                break;  
            case 2:  
                recordExpense();  
                break;  
            case 3:  
                printAllTransactions();  
                break;  
            case 4:  
                searchTransaction();  
                break;  
            case 5:  
                setBudget();  
                break;  
            case 6:  
                displayRemainingBudget();  
                break;  
            case 7:  
                displayMonthlyStatistics();  
                break;  
            case 0:  
                printf("退出程序。\n");  
                exit(0);  
            default:  
                printf("无效的选择，请重试。\n");  
        }  
    }  

    return 0;  
}  

void displayMenu() {  
    printf("\n=================================\n");  
    printf("欢迎使用个人账单管理系统\n");  
    printf("=================================\n");  
    printf("请选择操作：\n");  
    printf("1. 记录收入\n");  
    printf("2. 记录支出\n");  
    printf("3. 查看所有账单\n");  
    printf("4. 查询账单\n");  
    printf("5. 设置预算\n");  
    printf("6. 显示剩余预算\n");  
    printf("7. 显示月度统计\n");  
    printf("0. 退出\n");  
    printf("=================================\n");  
}  

void recordIncome() {  
    if (transactionCount >= MAX_TRANSACTIONS) {  
        printf("交易记录已满，无法添加更多记录。\n");  
        return;  
    }  

    Transaction income;  
    strcpy(income.type, "income");  
    printf("请输入收入描述: ");  
    scanf("%s", income.description);  
    printf("请输入收入金额: ");  
    scanf("%lf", &income.amount);  

    transactions[transactionCount++] = income;  
    printf("收入记录成功！\n");  
}  

void recordExpense() {  
    if (transactionCount >= MAX_TRANSACTIONS) {  
        printf("交易记录已满，无法添加更多记录。\n");  
        return;  
    }  

    Transaction expense;  
    strcpy(expense.type, "expense");  
    printf("请输入支出描述: ");  
    scanf("%s", expense.description);  
    printf("请输入支出金额: ");  
    scanf("%lf", &expense.amount);  

    transactions[transactionCount++] = expense;  
    monthlyBudget.totalSpent += expense.amount; // 更新总支出  
    printf("支出记录成功！\n");  
}  

void printAllTransactions() {  
    printf("\n所有账单记录:\n");  
    for (int i = 0; i < transactionCount; i++) {  
        printf("账单 %d: 类型: %s, 描述: %s, 金额: %.2lf\n", i + 1, transactions[i].type, transactions[i].description, transactions[i].amount);  
    }  
}  

void searchTransaction() {  
    char searchDescription[100];  
    printf("请输入要查询的账单描述: ");  
    scanf("%s", searchDescription);  
    clearInputBuffer(); // 清除输入缓冲区中的可能残留字符  

    bool found = false;  
    for (int i = 0; i < transactionCount; i++) {  
        if (strstr(transactions[i].description, searchDescription) != NULL) {  
            found = true;  
            printf("找到账单: 类型: %s, 描述: %s, 金额: %.2lf\n", transactions[i].type, transactions[i].description, transactions[i].amount);  
        }  
    }  

    if (!found) {  
        printf("未找到匹配的账单记录。\n");  
    }  
}  

// 设置预算  
void setBudget() {  
    printf("请输入每月预算限额: ");  
    scanf("%lf", &monthlyBudget.budget);  
    monthlyBudget.totalSpent = 0; // 重置已支出金额  
    printf("预算设置成功！每月预算: %.2lf\n", monthlyBudget.budget);  
}  

// 显示剩余预算  
void displayRemainingBudget() {  
    double remainingBudget = monthlyBudget.budget - monthlyBudget.totalSpent;  
    printf("当前剩余可用预算: %.2lf\n", remainingBudget);  
}  

// 显示月度统计  
void displayMonthlyStatistics() {  
    double totalIncome = 0;  
    double totalExpense = monthlyBudget.totalSpent;  

    for (int i = 0; i < transactionCount; i++) {  
        if (strcmp(transactions[i].type, "income") == 0) {  
            totalIncome += transactions[i].amount;  
        }  
    }  

    printf("本月统计:\n");  
    printf("总收入: %.2lf\n", totalIncome);  
    printf("总支出: %.2lf\n", totalExpense);  
    printf("剩余预算: %.2lf\n", monthlyBudget.budget - totalExpense);  
}  

// 清除输入缓冲区中的残留字符  
void clearInputBuffer() {  
    int c;  
    while ((c = getchar()) != '\n' && c != EOF) {}  
}  //gjgj